package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.mapper.ClassTableMapper;
import org.example.model.Dto.Student;
import org.example.model.Dto.TeacherDto;
import org.example.model.table.ClassTable;
import org.example.model.table.Course;
import org.example.model.vo.ClassVo;
import org.example.service.ClassTableService;
import org.example.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class ClassTableServiceImpl implements ClassTableService {
    @Autowired
    ClassTableMapper classTableMapper;

    @Override
    public Result getClassInfoById(Long id,int page ,int size) {

        IPage<ClassVo> classVoIPage = classTableMapper.selectJoinPage(new Page<ClassVo>(), ClassVo.class, new MPJLambdaWrapper<ClassTable>()
                .selectAs(ClassTable::getId, ClassVo::getClassId)
                .selectAs(ClassTable::getName, ClassVo::getClassName)
                .selectAs(Course::getName, ClassVo::getCourse)
                .selectAs(Course::getId, ClassVo::getCourseID)
                .innerJoin(Course.class, Course::getId, ClassTable::getId)
                .selectAs(TeacherDto::getName, ClassVo::getTeacherName)
                .innerJoin(TeacherDto.class, TeacherDto::getId, Course::getTeacherId)
                .selectAs(Student::getName, ClassVo::getStudentName)
                .selectAs(Student::getId, ClassVo::getStudentId)
                .innerJoin(Student.class, Student::getClassId, ClassTable::getId
                ).eq(ClassTable::getId, id));

        if(classVoIPage!=null&&classVoIPage.getRecords().size()>0) {
            return Result.ok().data("classInfo", classVoIPage);
        }
        return Result.ok().data("msg","暂无数据").setCode(404);
    }

    @Override
    public Result deleteClassById(Long id) {
        int i = classTableMapper.deleteById(id);
        return i == 1 ?
                Result.ok().data("msg", "成功删除") :
                Result.error().data("msg", "删除失败").setCode(501);
    }

    @Override
    public Result changeClass(ClassTable classTable) {
        int i = classTableMapper.updateById(classTable);
        return i == 1 ?
                Result.ok().data("msg", "成功替换") :
                Result.error().data("msg", "替换失败").setCode(501);
    }

    @Override
    public Result addClass(ClassTable classTable) {
        LambdaQueryWrapper<ClassTable> classTableQueryWrapper = new LambdaQueryWrapper<>();
        classTableQueryWrapper.eq(ClassTable::getName,classTable.getName());
        if (classTableMapper.selectOne(classTableQueryWrapper)==null) {
            classTableMapper.insert(classTable);
            return Result.ok()
                    .data("msg","Add Success");
        }
        return Result.error().data("msg","这个班级已经存在").setCode(400);
    }

    @Override
    public Result getAllClassInfo(int page, int size) {
        IPage<ClassVo> classVoIPage = classTableMapper.selectJoinPage(new Page<ClassVo>(page, size), ClassVo.class, new MPJLambdaWrapper<ClassTable>()
                .selectAs(ClassTable::getId, ClassVo::getClassId)
                .selectAs(ClassTable::getName, ClassVo::getClassName)
                .selectAs(Course::getName, ClassVo::getCourse)
                .selectAs(Course::getId, ClassVo::getCourseID)
                .innerJoin(Course.class, Course::getId, ClassTable::getId)
                .selectAs(TeacherDto::getName, ClassVo::getTeacherName)
                .innerJoin(TeacherDto.class, TeacherDto::getId, Course::getTeacherId)
                .selectAs(Student::getName, ClassVo::getStudentName)
                .selectAs(Student::getId, ClassVo::getStudentId)
                .innerJoin(Student.class, Student::getClassId, ClassTable::getId));
        if(classVoIPage!=null&&classVoIPage.getRecords().size()>0)
            return Result.ok().data("classInfo",classVoIPage);
        return Result.ok().data("msg","暂无数据").setCode(404);
    }

    @Override
    public Result getClassList() {
        List<ClassTable> classTables = classTableMapper.selectList(null);
        if(classTables!=null&&classTables.size()>0)
            return Result.ok().data("classList",classTables);
        return Result.ok().data("msg","暂无数据").setCode(404);
    }

    @Override
    public Result getClassPage(Integer page, Integer size, ClassTable classTable) {
        LambdaQueryWrapper<ClassTable> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(classTable.getName()), ClassTable::getName, classTable.getName());
        Page<ClassTable> classTablePage = classTableMapper.selectPage(new Page<>(page, size), queryWrapper);
        if(classTablePage!=null&&classTablePage.getRecords().size()>0)
            return Result.ok().data("classList",classTablePage);
        return Result.ok().data("msg","暂无数据").setCode(404);
    }
}
